package com.example.arffrecorder.graphics;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.example.arffrecorder.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.view.View;

public class RoomVisualization extends View{

	/** initialize arrays for offline and online users **/
	Paint paint = new Paint();
	static ArrayList<String> offlineUsers = new ArrayList<String>();
	static ArrayList<String> onlineUsers = new ArrayList<String>();
	static ArrayList<String> onlineRoles = new ArrayList<String>();

	static int roomState = 1;
	Map<String, String> user_state = new HashMap<String, String>();
	/** Positions of labels and icons of users **/
	static ArrayList<Integer> xPos = new ArrayList<Integer>();
	static ArrayList<Integer> yPos = new ArrayList<Integer>();

	/** Populate the position arrays **/
	public RoomVisualization(Context context) {
		super(context);
		for (int i = 70; i<800; i+=175)
			for (int it = 0; it<3; it++)
				xPos.add(i);
		for (int it = 0; it<3; it++)
			for (int i=220; i<480; i+=100)
				yPos.add(i);
		// TODO Auto-generated constructor stub
	}
	
	/** Method to draw the UI **/
	public void onDraw(Canvas canvas){
		/** Images for the different roles of users **/
		Bitmap speaker=BitmapFactory.decodeResource(getResources(), R.drawable.speaker);
		Bitmap listener=BitmapFactory.decodeResource(getResources(), R.drawable.listener);
		Bitmap transition=BitmapFactory.decodeResource(getResources(), R.drawable.transition);
		Bitmap offline=BitmapFactory.decodeResource(getResources(), R.drawable.offline);
		paint.setColor(Color.WHITE);
		paint.setTextSize(30);
		

		/** room state lecture **/
		if (roomState == 0){
			
			int cont = 0;
			Bitmap background=BitmapFactory.decodeResource(getResources(), R.drawable.classroom_lecture);
	        canvas.drawBitmap(background, 0, 0, paint);
			
	        int ind = onlineRoles.indexOf("speaker");
	        if (ind>-1){
				canvas.drawBitmap(speaker, 300, 50, paint);
				canvas.drawText(onlineUsers.get(ind), 280, 120, paint);
				
	        }
	        //onlineUsers.remove(ind);
			//onlineRoles.remove(ind);
			//
			for (int i=0; i<offlineUsers.size(); i++){
				canvas.drawText(offlineUsers.get(i), xPos.get(i), yPos.get(i), paint);
				canvas.drawBitmap(offline, xPos.get(cont)+30, yPos.get(cont)-70,paint);
				cont ++;
			}
			for (int i=0; i<onlineUsers.size(); i++){
				if (!(onlineRoles.get(i).equals("speaker"))){
					canvas.drawText(onlineUsers.get(i), xPos.get(cont), yPos.get(cont), paint);
					if (onlineRoles.get(i).equals("transition"))
						canvas.drawBitmap(transition, xPos.get(cont)+30, yPos.get(cont)-70,paint);
					else if (onlineRoles.get(i).equals("listener"))
						canvas.drawBitmap(listener, xPos.get(cont)+30, yPos.get(cont)-70, paint);
					else
						canvas.drawBitmap(speaker, xPos.get(cont)+30, yPos.get(cont)-100, paint);
					cont++;
				}
			}
			
		}
		/** room state empty **/
		else if (roomState == 1){
			int cont = 0;

			Bitmap background=BitmapFactory.decodeResource(getResources(), R.drawable.classroom_empty);
	        canvas.drawBitmap(background, 0, 0, paint);
		
					//
			for (int i=0; i<offlineUsers.size(); i++){
				canvas.drawText(offlineUsers.get(i), xPos.get(i), yPos.get(i), paint);
				canvas.drawBitmap(offline, xPos.get(i)+30, yPos.get(i)-70,paint);
				cont ++;
			}
			for (int i=0; i<onlineUsers.size(); i++){
				canvas.drawText(onlineUsers.get(i), xPos.get(i), yPos.get(i), paint);
				if (onlineRoles.get(i).equals("transition"))
					canvas.drawBitmap(transition, xPos.get(i)+30, yPos.get(i)-70,paint);
				else if (onlineRoles.get(i).equals("listener"))
					canvas.drawBitmap(listener, xPos.get(i)+30, yPos.get(i)-70,paint);
				else
					canvas.drawBitmap(speaker, xPos.get(i)+30, yPos.get(i)-100,paint);
				cont ++;

			}
		}
		/** room state transition **/
		else if (roomState == 2){
			int cont = 0;
			Bitmap background=BitmapFactory.decodeResource(getResources(), R.drawable.classroom_transition);
	        canvas.drawBitmap(background, 0, 0, paint);
		
		
			
			for (int i=0; i<offlineUsers.size(); i++){
				canvas.drawText(offlineUsers.get(i), xPos.get(i), yPos.get(i), paint);
				canvas.drawBitmap(offline, xPos.get(i)+30, yPos.get(i)-40,paint);
				cont ++;
			}

			for (int i=0; i<onlineUsers.size(); i++){
				canvas.drawText(onlineUsers.get(i), xPos.get(i), yPos.get(i), paint);
				if (onlineRoles.get(i).equals("transition"))
					canvas.drawBitmap(transition, xPos.get(i)+30, yPos.get(i)-70,paint);
				else if (onlineRoles.get(i).equals("listener"))
					canvas.drawBitmap(listener, xPos.get(i)+30, yPos.get(i)-70,paint);
				else
					canvas.drawBitmap(speaker, xPos.get(i)+30, yPos.get(i)-100,paint);
				cont ++;
			}
		}
		//canvas.drawColor(Color.GREEN);
		
	}

}
